from apps.common.base.dao import BaseDao
from apps.education.section.serializers import *

class SectionDao(BaseDao):
    MODEL_CLASS = Section
    SERI_CLASS = SectionBaseSerializer


    def build_tree(self, nodes):
        # 构建一个映射 id -> node
        node_map = {node['id']: node for node in nodes}
        
        # 添加 children 字段
        for node in nodes:
            node['children'] = []

        # 构建树
        tree = []
        for node in nodes:
            parent = node['parent']
            if parent and parent in node_map:
                node_map[parent]['children'].append(node)
            else:
                tree.append(node)
        return tree